package com.itheima.restkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.restkeeper.basic.BasicPojo;
import com.itheima.restkeeper.constant.SuperConstant;
import com.itheima.restkeeper.pojo.Store;
import com.itheima.restkeeper.mapper.StoreMapper;
import com.itheima.restkeeper.req.StoreVo;
import com.itheima.restkeeper.service.IStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.restkeeper.utils.BeanConv;
import com.itheima.restkeeper.utils.EmptyUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description：门店信息账号 服务实现类
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

    /**
     * 分页查询门店列表
     * @param storeVo 查询条件
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return
     */
    @Override
    public Page<Store> findStoreVoPage(StoreVo storeVo, int pageNum, int pageSize) {
        //分页条件构建
        Page<Store> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Store> queryWrapper = Wrappers.<Store>lambdaQuery();
        //按门店名称查询
        if (!EmptyUtil.isNullOrEmpty(storeVo.getStoreName())) {
            queryWrapper.like(Store::getStoreName,storeVo.getStoreName());
        }
        //按门店状态查询
        if (!EmptyUtil.isNullOrEmpty(storeVo.getEnableFlag())) {
            queryWrapper.eq(Store::getEnableFlag,storeVo.getEnableFlag());
        }
        //返回最终结果
        return page(page,queryWrapper);
    }

    /**
     * 新增一个门店
     * @param storeVo 对象信息
     * @return
     */
    @Override
    public Store createStore(StoreVo storeVo) {
        Store store = BeanConv.toBean(storeVo, Store.class);
        return save(store)?store:null;
    }

    /**
     * 修改门店
     * @param storeVo 对象信息
     * @return
     */
    @Override
    public Boolean updateStore(StoreVo storeVo) {
        return updateById(BeanConv.toBean(storeVo,Store.class));
    }

    /**
     * 删除门店
     * @param checkedIds 选择的门店ID
     * @return
     */
    @Override
    public Boolean deleteStore(String[] checkedIds) {
        //将数组转化为集合
        List<String> list = Arrays.asList(checkedIds);
        List<Long> longs = new ArrayList<>();
        list.forEach(n->{
            longs.add(Long.valueOf(n));
        });
        return removeByIds(longs);
    }

    /**
     * 查询门店下拉列表
     * @return
     */
    @Override
    public List<Store> findStoreVoList() {
        return list(Wrappers.<Store>lambdaQuery().eq(Store::getEnableFlag,SuperConstant.YES));
    }

}
